#include "register.h"
#include "ui_register.h"
#include <QRegExpValidator>
#include "networkcontroller.h"
#include <QMessageBox>
#include "verify.h"
#include "loginpage.h"


Register::Register(QWidget *parent) :
    QDialog(parent),
    timer(new QTimer(this)),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    int num =10;
    connect(timer,&QTimer::timeout,[&](){
        if(num > 0){
            qDebug()<<"1";
            num--;
            ui->btn_sc->setText(QString("send code %1").arg(QString::number(num)));
        }
        else
        {
            ui->btn_sc->setEnabled(true);
            ui->btn_sc->setText("send code");
            timer->stop();
            num=60;
        }
    });
}

bool isValidEmail(const QString& email) {
    // 使用正则表达式验证邮箱格式
    QRegularExpression regex(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b)");
    QRegularExpressionMatch match = regex.match(email);
    return match.hasMatch();
}

QString checkPasswordRequirements(const QString &password) {
    QString message=QString("");

    // 检查是否包含大写字母
    if (!QRegularExpression("(?=.*[A-Z])").match(password).hasMatch()) {
        message += "- 密码必须包含至少一个大写字母\n";
    }

    // 检查是否包含小写字母
    if (!QRegularExpression("(?=.*[a-z])").match(password).hasMatch()) {
        message += "- 密码必须包含至少一个小写字母\n";
    }

    // 检查是否包含数字
    if (!QRegularExpression("(?=.*\\d)").match(password).hasMatch()) {
        message += "- 密码必须包含至少一个数字\n";
    }

    // 检查是否包含特殊字符
    if (!QRegularExpression("(?=.*[!@#$%^&*()_+\\-=\\[\\]{};:\"\\\\|,.<>\\/?])").match(password).hasMatch()) {
        message += "- 密码必须包含至少一个特殊字符\n";
    }

    // 检查长度
    if (!QRegularExpression("[a-zA-Z\\d!@#$%^&*()_+\\-=\\[\\]{};:\"\\\\|,.<>\\/?]{8,16}").match(password).hasMatch()) {
        message += "- 密码长度必须在8到16个字符之间\n";
    }

    return message;
}

Register::~Register()
{
    delete ui;
}

void Register::on_btn_continue_clicked()
{
    if(!LoginPage::tryConnect())
    {
        return;
    }
    Verify* verify = new Verify(this);
    verify->exec();
    if(!verify->getIsVerified())
    {
        return;
    }

    if(ui->lb_tun->text()==""&&
            ui->lb_tpw->text()==""&&
            ui->lb_tcpw->text()==""&&
            ui->le_vc->text()==code)
    {
        QString password =toMd5(ui->le_password->text());
        QString username =ui->le_username->text();
        NetWorkController::getInstance()->sendRegisterMessage(username,password);
    }
    else
    {
        QMessageBox::warning(nullptr,tr("错误"),tr("登录失败"));
    }
}


void Register::on_le_c_password_textChanged(const QString &arg1)
{
    if(ui->le_password->text()!=ui->le_c_password->text())
    {
        ui->lb_tcpw->setText(QString("两次输入密码不一致!"));
        ui->lb_tcpw->setStyleSheet("color:red;");
        ui->lb_cpw->setStyleSheet("color:red;");
        ui->lb_cpw->setText("Confirm Password *");
    }
    else if(ui->le_password->text()==ui->le_c_password->text()||ui->le_password->text()==QString(""))
    {
        ui->lb_tcpw->setText(QString(""));
        ui->lb_tcpw->setStyleSheet("color:white;");
        ui->lb_cpw->setStyleSheet("color:white;");
        ui->lb_cpw->setText("Confirm Password");
    }
}


void Register::on_le_password_textChanged(const QString &arg1)
{
    ui->lb_tpw->setText(checkPasswordRequirements(ui->le_password->text()));
    if(ui->lb_tpw->text().length()!=0)
    {
        ui->lb_pw->setStyleSheet("color:red");
        ui->lb_pw->setText("PassWord *");
        ui->lb_tpw->setStyleSheet("color:red");
    }
    else
    {
        ui->lb_pw->setStyleSheet("color:white");
        ui->lb_pw->setText("PassWord");
        ui->lb_tpw->setStyleSheet("color:white");
    }

}


void Register::on_le_username_textChanged(const QString &arg1)
{
    if(!isValidEmail(ui->le_username->text()))
    {
        ui->lb_tun->setText("邮箱格式不正确");
        ui->lb_tun->setStyleSheet("color:red");
        ui->lb_un->setText("UserName *");
        ui->lb_un->setStyleSheet("color:red");
    }else{
        ui->lb_tun->setText("");
        ui->lb_tun->setStyleSheet("color:white");
        ui->lb_un->setText("UserName");
        ui->lb_un->setStyleSheet("color:white");
    }

}


void Register::on_btn_eye_pressed()
{
    ui->le_password->setEchoMode(QLineEdit::Normal);
}


void Register::on_btn_eye_released()
{
    ui->le_password->setEchoMode(QLineEdit::Password);
}


void Register::on_btn_sc_clicked()
{
    QString emailAddr = ui->le_username->text();
    code = NetWorkController::getInstance()->generateCaptcha();
    NetWorkController::getInstance()->sendCaptchaEmail(emailAddr,code);
    ui->btn_sc->setEnabled(false);
    QString btnText = QString("send code 10");
    ui->btn_sc->setText(btnText);
    timer->start(1000);
}

QString Register::toMd5(const QString &str)
{
    QString front = "123";
    QString behind = "456";
    QString newStr =QString(front + str + behind) ;
    QCryptographicHash md5(QCryptographicHash::Md5);
    md5.addData(newStr.toLocal8Bit());
    return md5.result().toHex();
}

